// pages/rechargeOrderList/rechargeOrderList.js
const app = getApp()
import {
  postRequest,
} from '../../api/request'
import moment from '../../utils/moment';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeColor: app.globalData.themeColor,
    subColor: app.globalData.subColor,
    btnColor: app.globalData.btnColor,
    dataType: 0,
    tabList: [{
        id: 0,
        name: "充值订单"
      },
      {
        id: 1,
        name: "余额账单"
      },
    ],
    orderList: [],
    pageNo: 1,
    pageSize: 10,
    loadType: "",
  },
  //tab切换时
  tabSelect(e) {
    this.setData({
      dataType: e.currentTarget.dataset.id,
    })
    this.initData();
  },
  //初始化数据
  initData() {
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 10
    })
    this.setData({
      pageNo: 1,
      pageSize: 10,
      loadType: "",
    });
    this.getOrderList(false);
  },

  //获取订单列表,isConcat 是否拼接
  getOrderList(isConcat) {
    let that = this;
    //判断是储值还是消费
    if (this.data.dataType == 0) {
      postRequest("/api/vip/getRechargeOrderList", {
        beid: app.beid,
        memberId: app.myInfo.memberId,
        pageNo: that.data.pageNo,
        pageSize: that.data.pageSize,
        keyword: "",
        orderStatus: "",
        refundStatus: "",
        startTime: "",
        endTime: "",
      }).then((res) => {
        console.log(res.data.data);
        if (res.data.data != null) {
          res.data.data.data.forEach((item, index) => {
            item.createTime = moment(item.createTime).format('YY年MM月DD日 HH:mm')
          })
          //合并数据
          let list = isConcat ? that.data.orderList.concat(res.data.data.data) : res.data.data.data;
          console.log(list);
          //判断是否还有数据
          let loadType = "";
          if (res.data.data.totalCount == 0) {
            loadType = "loadNull";
          } else {
            if (Math.ceil(res.data.data.totalCount / that.data.pageNo) <= that.data.pageSize) {
              loadType = "loadOver";
            } else {
              loadType = "loadMore";
            }
          }
          that.setData({
            orderList: list,
            loadType: loadType,
            pageNo: that.data.pageNo + 1,
          })
          // 停止下拉刷新效果
          wx.stopPullDownRefresh()
        }
      }).catch((err) => {
        // 停止下拉刷新效果
        wx.stopPullDownRefresh();
      });
    } else {
      postRequest("/api/vip/getRechargeConsumeList", {
        beid: app.beid,
        memberId: app.myInfo.memberId,
        pageNo: that.data.pageNo,
        pageSize: that.data.pageSize,
        keyword: "",
        startTime: "",
        endTime: "",
      }).then((res) => {
        console.log(res.data.data);
        if (res.data.data != null) {
          res.data.data.data.forEach((item, index) => {
            item.createTime = moment(item.createTime).format('YY年MM月DD日 HH:mm')
          })
          //合并数据
          let list = isConcat ? that.data.orderList.concat(res.data.data.data) : res.data.data.data;
          console.log(list);
          //判断是否还有数据
          let loadType = "";
          if (res.data.data.totalCount == 0) {
            loadType = "loadNull";
          } else {
            if (Math.ceil(res.data.data.totalCount / that.data.pageNo) <= that.data.pageSize) {
              loadType = "loadOver";
            } else {
              loadType = "loadMore";
            }
          }

          that.setData({
            orderList: list,
            loadType: loadType,
            pageNo: that.data.pageNo + 1,
          })
          // 停止下拉刷新效果
          wx.stopPullDownRefresh()
        }
      }).catch((err) => {
        // 停止下拉刷新效果
        wx.stopPullDownRefresh();
      });
    }

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.dataType != "" && options.dataType != undefined) {
      this.setData({
        dataType: options.dataType,
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    //如果没有token,则需要重新登录获取个人信息等，避免缺少id导致数据无法获取
    if (app.token == "" || app.token == undefined) {
      app.initLogin().then(res => {
        this.initData();
      }).catch(err => {})
    } else {
      this.initData();
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.initData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.loadType == "loadMore") {
      this.setData({
        loadType: "",
      });
      this.getOrderList(true);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})